package me.zhengjie.org.employeechange.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.org.employeechange.domain.EmployeeChange;
import me.zhengjie.org.employeechange.repository.EmployeeChangeRepository;
import me.zhengjie.org.employeechange.service.EmployeeChangeService;
import me.zhengjie.org.employeechange.service.dto.EmployeeChangeQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
//import me.zhengjie.org.employeechange.repository;
/**
* @author ZGZ
* @date 2020-04-14
*/
@Api(tags = "员工变动管理")
@RestController
@RequestMapping("/api/employeeChange")
public class EmployeeChangeController {

    private final EmployeeChangeService employeeChangeService;

    @Autowired
    private  EmployeeChangeRepository employeeChangeRepository;

    public EmployeeChangeController(EmployeeChangeService employeeChangeService) {
        this.employeeChangeService = employeeChangeService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('employeeChange:list')")
    public void download(HttpServletResponse response, EmployeeChangeQueryCriteria criteria) throws IOException {
        employeeChangeService.download(employeeChangeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询员工变动")
    @ApiOperation("查询员工变动")
    @PreAuthorize("@el.check('employeeChange:list')")
    public ResponseEntity<Object> getEmployeeChanges(EmployeeChangeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(employeeChangeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增员工变动")
    @ApiOperation("新增员工变动")
    @PreAuthorize("@el.check('employeeChange:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody EmployeeChange resources){
        return new ResponseEntity<>(employeeChangeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改员工变动")
    @ApiOperation("修改员工变动")
    @PreAuthorize("@el.check('employeeChange:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EmployeeChange resources){
        if ( resources.get_Id() == null) {
            employeeChangeRepository.insertEmployeeChange(resources.get_empId(), resources.get_fromCenterId(), resources.get_toCenterId(), resources.get_changeTime(), resources.get_describe());
        }
        else {
            employeeChangeService.update(resources);
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除员工变动")
    @ApiOperation("删除员工变动")
    @PreAuthorize("@el.check('employeeChange:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        employeeChangeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}